  // const openURL = require('./openURL')
  
  function isPortOccupiedExpress(app,port){
    return new Promise((resolve, reject) => {
        const server = app.listen(port)
        const duration = 2000 // 重试间隔
        let left = 5 // 重试次数
        let showErrMsg = true
    
        server.on('listening', () => {
          resolve(`app listening at http://localhost:${port}`)
          console.log(`app listening at http://localhost:${port}`)
          // openURL(`http://localhost:${port}`)
        })
        server.on('error', (error) => {
          if (error.code !== 'EADDRINUSE') { // 系统非监听端口操作报错
            throw error
          }
    
          left = left - 1
          if (showErrMsg) {
            console.log(`port ${port} already in use`)
            showErrMsg = false
          }
          console.log(`trying to restart the service on port ${port}... attempts left ${left} `)
          if (left !== 0) {
            setTimeout(() => server.listen(port), duration)
          } else {
            reject('Server is shutting down')
          }
        })
      })
}
module.exports = isPortOccupiedExpress